package com.nit.lab.controller.basic;


import com.alibaba.fastjson.JSONObject;
import com.nit.lab.dto.basic.StaffDTO;
import com.nit.lab.dto.system.TokenDTO;
import com.nit.lab.dto.utils.OpResultDTO;
import com.nit.lab.dto.utils.TableReqDTO;
import com.nit.lab.dto.utils.TableRspDTO;
import com.nit.lab.service.basic.StaffService;
import com.nit.lab.utils.HttpRequestReader;
import com.nit.lab.utils.JWTUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping(value = "/api/staff")
public class StaffController {

    @Resource
    private StaffService staffService;
    private final Logger logger = LoggerFactory.getLogger(StaffController.class);


    @RequestMapping(value = "/query", method = RequestMethod.POST)
    public TableRspDTO list4Table(@RequestBody TableReqDTO tableReqDTO) {
        TableRspDTO tableRspDTO = new TableRspDTO();
        try {
            tableRspDTO = staffService.list4Table(tableReqDTO);
        } catch (Exception e) {
            logger.error(e.toString());
        }
        return tableRspDTO;
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public OpResultDTO add(@RequestBody StaffDTO staffDTO) {
        OpResultDTO opResult = new OpResultDTO();
        try {
            opResult.setMessage(staffService.add(staffDTO).toString());
        } catch (Exception e) {
            logger.error(e.toString());
            opResult.setMessage("error");
        }
        return opResult;
    }


    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public OpResultDTO update(@RequestBody StaffDTO staffDTO) {
        OpResultDTO opResult = new OpResultDTO();
        try {
            opResult.setMessage(staffService.update(staffDTO).toString());
        } catch (Exception e) {
            logger.error(e.toString());
            opResult.setMessage("error");
        }
        return opResult;
    }


    @RequestMapping(value = "/remove", method = RequestMethod.POST)
    public OpResultDTO remove(HttpServletRequest request) {
        OpResultDTO opResult = new OpResultDTO();
        try {
            JSONObject jsonRequest = HttpRequestReader.getJsonObject(request);
            String staffIds = jsonRequest.getString("staffIds");
            opResult.setMessage(staffService.remove(staffIds).toString());
        } catch (Exception e) {
            logger.error(e.toString());
            opResult.setMessage("error");
        }
        return opResult;
    }

    @RequestMapping(value = "/getStaffById", method = RequestMethod.POST)
    public OpResultDTO getStaffById(HttpServletRequest request) {
        OpResultDTO opResult = new OpResultDTO();
        try {
            JSONObject jsonRequest = HttpRequestReader.getJsonObject(request);
            String staffId = jsonRequest.getString("staffId");
            opResult.setMessage("success");
            opResult.setResult(staffService.getStaffById(Long.parseLong(staffId)));
        } catch (Exception e) {
            logger.error(e.toString());
            opResult.setMessage("error");
        }
        return opResult;
    }
}
